package ajaxjs.data.ext;

import java.io.File;
import java.text.SimpleDateFormat;

import ajaxjs.admin.entry.CRUD;
import ajaxjs.data.entry.Entry;
import ajaxjs.data.service.BaseService;
import ajaxjs.data.service.subObject.Img;
import ajaxjs.net.upload.Upload_HTTP;
import ajaxjs.sql.DAO;

public class VodService extends BaseService {

	public static String name = "点播";
	public static String id = "vod";
	
	public Entry pojo = new Vod();
	
	// 只有 方法 才可以获取当前实例的
	public Entry getPojo(){
		return this.pojo;
	}
	
	@Override
	public String getTableName() {
		return id;
	}
	public BaseService[] subObjects = {
		new Img(){
			@Override
			public String getParentTableName() {
				return id;
			}
		}
	};

	@Override
	public String getName() {
		return name;
	}
	@Override
	public String getId() {
		return id;
	}
	
	private static String mkdir(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
		return sdf.format(new java.util.Date());  
	}
	
	public String upload(){
		String folder = mkdir();
		Upload_HTTP uploader = new Upload_HTTP(request);
		uploader.setSaveFolder("content" + File.separator + folder);
		uploader.upload();
		
//		Connection jdbcConn = DAO.getConn("jdbc/mysql_deploy");
		DAO dao = new DAO("com.mysql.jdbc.Driver", "jdbc:mysql://221.4.246.90:3306/zanjiang?user=root&password=2013@tech", null);
		
		
		CRUD crud = new CRUD(dao.jdbcConn){
			public String getTableName(){
				return "video";
			}
		};
		
		int newlyId = crud.create(
			new String[]{"contentid", "contentname", "filepath", "transcodestatus"}, 
			new String[]{
				request.getParameter("id"), "'" + uploader.fileName + "'", "'" + folder + "/" + uploader.fileName + "'", "0",
			}
		);
//		
		
		System.out.println("wrote ok "+ newlyId);
		dao.closeConnection();
		return newlyId + "#" + uploader.fileName;
	}
}